#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

/**
 * PAT A 1012: The Best Rank
 */

char courseNames[4] = {'A', 'C', 'M', 'E'};

struct Student {
    int idx;
    int scores[4];
    int best;
    int bestCourse;

    Student(int idx, int c, int m, int e)
        : idx(idx), scores{(c + m + e) / 3, c, m, e}, best(INT32_MAX) {}

    bool operator<(const Student &other) const {
        return idx < other.idx;
    }
};

struct ScoreCmp {
    int course;

    ScoreCmp(int course) : course(course) {}

    bool operator()(Student &a, Student &b) const {
        return a.scores[course] > b.scores[course];
    }
};

int main() {
    // Read data
    int nStudents, nQueries;
    scanf("%d %d", &nStudents, &nQueries);
    std::vector<Student> students;
    std::unordered_map<int, int> idToIdx;
    for (int i = 0; i < nStudents; i++) {
        int id, c, m, e;
        scanf("%d %d %d %d", &id, &c, &m, &e);
        int idx = students.size();
        students.push_back(Student(idx, c, m, e));
        idToIdx.insert(std::make_pair(id, idx));
    }

    // Sort according to four metrics and update rank
    for (int c = 0; c < 4; c++) {
        ScoreCmp cmp(c);
        std::sort(students.begin(), students.end(), cmp);
        int prev = 0;
        for (int i = 0; i < nStudents; i++) {
            Student &stu = students[i];
            int rank = i;
            if (i > 0 && stu.scores[c] == students[prev].scores[c])
                rank = prev;
            else
                prev = i;
            if (rank < stu.best) {
                stu.best = rank;
                stu.bestCourse = c;
            }
        }
    }

    // Respond to queries
    std::sort(students.begin(), students.end());
    for (int i = 0; i < nQueries; i++) {
        int id;
        scanf("%d", &id);
        if (idToIdx.find(id) == idToIdx.end()) {
            printf("N/A\n");
            continue;
        }
        Student &stu = students[idToIdx[id]];
        printf("%d %c\n", stu.best + 1, courseNames[stu.bestCourse]);
    }

    return 0;
}